package net.yyge.manifest.editor.tag;

import org.w3c.dom.*;

import java.util.LinkedHashMap;
import java.util.Map;

public class ActivityTag extends XmlTag {
    Map<String, Element> data = new LinkedHashMap<>();
    private Document document = null;

    private Node mainIntentFilter;


    public ActivityTag(Document document) {
        this.document = document;
    }

//    public ActivityTag(Node node) {
//        this.node = node;
//        element = (Element) node;
//        NamedNodeMap attrMap = element.getAttributes();
//        putData(attrMap, data);
//    }

    public ActivityTag setValue(String activityName, String attrName, String value) {
        Element element = data.get(activityName);
        if (element == null) {
            setValue(element, attrName, value);
        }
        return this;
    }


    public ActivityTag addAttr(String activityName, String attrName, String value) {
        Element element = data.get(activityName);
        if (element == null) {
            addAttribute(element, attrName, value);
        }
        return this;
    }

    public ActivityTag initActivity(Element element) {
        NamedNodeMap attributes = element.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attribute = attributes.item(i);
            if ("android:name".equals(attribute.getNodeName())) {
                Node mainAction = findMainAction(element);
                if(mainAction != null){
                    mainIntentFilter = mainAction;
                }
                data.put(attribute.getNodeValue(), element);
            }
        }
        return this;
    }


    /**
     * 查找  <action android:name="android.intent.action.MAIN"></action> 的父类
     *
     * @param element 当前的Activity
     * @return action 的父类 Element
     */
    private Node findMainAction(Element element) {
        NodeList actions = element.getElementsByTagName("action");
        for (int i = 0; i < actions.getLength(); i++) {
            Element action = (Element) actions.item(i);
            String value = getAttrValue(action.getAttributes(), "android:name");
            if (value == null) {
                return null;
            }
            if ("android.intent.action.MAIN".equals(value)) {
                return action.getParentNode();
            }
        }
        return null;
    }

    public ActivityTag setMainActivity(String activityName) {
        Element element = data.get(activityName);
        if (element == null) {
            throw new IllegalArgumentException("Activity not found: " + activityName);
        }
        Element intentFilter = document.createElement("intent-filter");
        Element action = document.createElement("action");
        action.setAttribute("android:name", "android.intent.action.MAIN");

        Element category = document.createElement("category");
        category.setAttribute("android:name", "android.intent.category.LAUNCHER");

        intentFilter.appendChild(action);
        intentFilter.appendChild(category);
        element.appendChild(intentFilter);


        //删除原有的MainActivity标签intent-filter标签内容
        if (mainIntentFilter != null) {
            mainIntentFilter.getParentNode().removeChild(mainIntentFilter);
        }
        //替换原有的MainIntentFilter
        mainIntentFilter = (Node) intentFilter;
        return this;
    }
}
